package com.sav.common.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.sav.common.forms.ProductForm;

public class ProductValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return ProductForm.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object obj, Errors err) {
		ProductForm productForm = (ProductForm)obj;
		ValidationUtils.rejectIfEmptyOrWhitespace(err, "name", "name.empty", "Name must not be empty.");
		ValidationUtils.rejectIfEmptyOrWhitespace(err, "description", "description.empty", "Description must not be empty.");
		ValidationUtils.rejectIfEmptyOrWhitespace(err, "cost", "cost.empty", "Cost must not be empty.");
		ValidationUtils.rejectIfEmptyOrWhitespace(err, "weight", "weight.empty", "Weight must not be empty.");
		ValidationUtils.rejectIfEmptyOrWhitespace(err, "quantity", "quantity.empty", "Quantity must not be empty.");
		
		String name = productForm.getName();
		if (name.length() > 128) {
			err.rejectValue("name", "name.tooLong", "Name must not more than 128 characters.");
		}
		/*SignupForm signupForm = (SignupForm) target;
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "username.empty", "Username must not be empty.");
		String username = signupForm.getUsername();
		if ((username.length()) > 16) {
			errors.rejectValue("username", "username.tooLong", "Username must not more than 16 characters.");
		}
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.empty", "Password must not be empty.");
		if (!(signupForm.getPassword()).equals(signupForm
				.getConfirmPassword())) {
			errors.rejectValue("confirmPassword", "confirmPassword.passwordDontMatch", "Passwords don't match.");
		}
		
		if( !EmailValidator.getInstance().isValid( signupForm.getEmail() ) ){
			errors.rejectValue("email", "email.notValid", "Email address is not valid.");
		} */
		
	}
	
}
